package com.wfm;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Service
public class CustomUserDetailsService implements UserDetailsService {
    private final Map<String, UserDetails> userRegistry = new HashMap<>();
    @PostConstruct
    public void init() {
        UserDetails user = User.withDefaultPasswordEncoder().username("wfm").password("wfm")
                .authorities("USER").build();
        UserDetails user1 = User.withDefaultPasswordEncoder().username("admin").password("admin")
                .authorities("ADMIN").build();
        userRegistry.put("wfm", user);
        userRegistry.put("admin", user1);
    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 生产这里是去数据库查询用户
        UserDetails userDetails = userRegistry.get(username);
        if (userDetails == null) {
            throw new UsernameNotFoundException(username);
        }
        return userDetails;
    }
}
